package com.wsc.wisdomchina.activitity.EditFunction;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.TextView;

import com.wsc.wisdomchina.DaoUtils.IndexNameSortDaoUtils;
import com.wsc.wisdomchina.Entity.IndexNameSort;
import com.wsc.wisdomchina.R;
import com.wsc.wisdomchina.Tools.JsonTools;
import com.wsc.wisdomchina.Tools.Tools;
import com.wsc.wisdomchina.activitity.baseactivity.BaseActivity;
import com.wsc.wisdomchina.adapter.DragGridViewAdapter;
import com.wsc.wisdomchina.adapter.EditFunctionAdapter;
import com.wsc.wisdomchina.callback.ContributorView;
import com.wsc.wisdomchina.constant.Constant;
import com.wsc.wisdomchina.javaBean.ItemSectionListBean;
import com.wsc.wisdomchina.javaBean.MyShowItemListBean;
import com.wsc.wisdomchina.javaBean.RespondObj;
import com.wsc.wisdomchina.presenter.ContributorPresenter;
import com.wsc.wisdomchina.view.DragGridView;
import com.wsc.wisdomchina.view.MyToast;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * author: xw
 * created on: 2017/10/28 12:00
 * description:
 */
public class IndexEditFunctionActivity extends BaseActivity implements ContributorView{

    private String cidNumber;
    private ContributorPresenter contributorPresenter;
    private final String ITEMSECTIONFLAG="ITEMSECTIONFLAG";
    private final String SAVEMYSECTIONFLAG="SAVEMYSECTIONFLAG";
    private Context context;
    private List<ItemSectionListBean> itemSectionListBeen;//其他功能List数据集合
    private GridView gv_other;
    private TextView tv_cancel;
    private TextView tv_finish;
    private boolean isLogin;
    private String userid;
    private IndexNameSortDaoUtils indexNameSortDaoUtils;
    private DragGridView dragGridView;
    private List<String> idList;//自选的Id集合
    private DragGridViewAdapter dragGridViewAdapter;
    private EditFunctionAdapter editFunctionAdapter;
    private LinkedList<MyShowItemListBean> homeModuleFourList;//更换功能数据集合
    private List<Integer> daiAddSort;//待添加的View
    private MyToast myToast;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.partychatinfodetail_layout);
        context=this;
        myToast=new MyToast(context);
        initView();
        Intent intent=getIntent();
        daiAddSort=new ArrayList<>();
        cidNumber=intent.getStringExtra("cid");
        contributorPresenter=new ContributorPresenter(this,context);

        isLogin= Tools.getSharedPreferencesboolen(Constant.isLoginIn,false,context);
        if(isLogin) {
            userid = Tools.getSharedPreferencesStrings(Constant.SharePreferenceUserid, "", context);
        }else {
            userid=cidNumber;
        }
        indexNameSortDaoUtils=new IndexNameSortDaoUtils(context);
        idList=new ArrayList<>();
        List<IndexNameSort> listIndex=indexNameSortDaoUtils.queryIndexNameSortByQueryBuilder(userid);
        if(listIndex!=null &&listIndex.size()>0)
        {
            IndexNameSort indexNameSort=listIndex.get(0);
            String message=indexNameSort.getObjMessage();
            homeModuleFourList = JsonTools.jsonStringToLinkList(message, MyShowItemListBean.class);
            dragGridViewAdapter=new DragGridViewAdapter(context,homeModuleFourList);
            dragGridView.setAdapter(dragGridViewAdapter);
            for(MyShowItemListBean myShowItemListBean:homeModuleFourList)
            {
                idList.add(myShowItemListBean.getMenuId());
                //待添加的View
                if(myShowItemListBean.getMenuId().equals("-1"))
                {
                    daiAddSort.add(homeModuleFourList.indexOf(myShowItemListBean));
                }
            }

            if(dragGridViewAdapter!=null)
            {
                //ListView item 中的删除按钮的点击事件
                dragGridViewAdapter.setOnItemDeleteClickListener(new DragGridViewAdapter.onItemDeleteListener() {
                    @Override
                    public void onDeleteClick(int i) {
                        idList.remove(homeModuleFourList.get(i).getMenuId());
                        homeModuleFourList.remove(i);
                        homeModuleFourList.add(i,new MyShowItemListBean("-1",i+1));
                        dragGridViewAdapter.notifyDataSetChanged();
                        daiAddSort.add(i);
                        if(editFunctionAdapter!=null)
                            editFunctionAdapter.notifyDataSetChanged();
                    }
                });

                dragGridViewAdapter.setOnswapView(new DragGridViewAdapter.OnswapView() {
                    @Override
                    public void onSwapView() {
                        homeModuleFourList=dragGridViewAdapter.getHomeModuleFourList();
                    }
                });
            }
        }
        contributorPresenter.getShowItemSections(cidNumber,ITEMSECTIONFLAG);

    }

    private void initView()
    {
        dragGridView=(DragGridView) findViewById(R.id.fragment_first_head_sida_gongnneg);
        gv_other=(GridView) findViewById(R.id.gv_other);
        tv_cancel=(TextView) findViewById(R.id.tv_cancel);
        tv_finish=(TextView) findViewById(R.id.tv_finish);
        tv_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        tv_finish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(homeModuleFourList!=null&& homeModuleFourList.size()>0)
                {
                    String menuId="";
                    for(MyShowItemListBean myShowItemListBean:homeModuleFourList)
                    {
                        menuId+=myShowItemListBean.getMenuId();
                        menuId+=",";
                    }
                    if(isLogin)
                    {
                        //登录状态保存到服务器
                        contributorPresenter.postSaveMySection(userid,menuId,SAVEMYSECTIONFLAG);
                    }else {
                        // 保存到本地数据库
                        IndexNameSort indexNameSort=indexNameSortDaoUtils.queryMeiziById(Constant.daoLocal);
                        if(indexNameSort!=null)
                        {
                            indexNameSortDaoUtils.updateIndexNameSort(new IndexNameSort(Constant.daoLocal,cidNumber,JsonTools.objectToJsonString(homeModuleFourList)));
                        }else {
                            indexNameSortDaoUtils.insertIndexNameSort(new IndexNameSort(Constant.daoLocal,cidNumber,JsonTools.objectToJsonString(homeModuleFourList)));
                        }
                    }

                }
            }
        });
    }

    @Override
    public void onLoadContributorSuccess(String jsonString, String flag) {
        try {
            if(flag.equals(ITEMSECTIONFLAG))
            {
                RespondObj respObj = JsonTools.jsonStringToObject(jsonString, RespondObj.class);
                String code = respObj.getCode();
                Object object = respObj.getObj();
                if (code.equals("200") && object != null) {
                    String jsonMessage = JsonTools.objectToJsonString(object);
                    itemSectionListBeen = JsonTools.jsonStringToList(jsonMessage, ItemSectionListBean.class);
                    if (itemSectionListBeen != null && itemSectionListBeen.size() > 0) {
                        editFunctionAdapter = new EditFunctionAdapter(context, itemSectionListBeen,idList);
                        gv_other.setAdapter(editFunctionAdapter);
                        if(editFunctionAdapter!=null)
                        {
                            editFunctionAdapter.setOnItemDeleteClickListener(new EditFunctionAdapter.onItemDeleteListener() {
                                @Override
                                public void onDeleteClick(int i) {
                                    if(daiAddSort.size()>0)
                                    {
                                        int sort=daiAddSort.get(0);
                                        daiAddSort.remove(0);
                                        homeModuleFourList.remove(sort);
                                        ItemSectionListBean itemSectionListBean=itemSectionListBeen.get(i);
                                        MyShowItemListBean myShowItemListBean=new MyShowItemListBean();
                                        myShowItemListBean.setMenuId(itemSectionListBeen.get(i).getId());
                                        MyShowItemListBean.MenuserverBean menuserverBean=new MyShowItemListBean.MenuserverBean();
                                        menuserverBean.setIcon(itemSectionListBean.getIcon());
                                        menuserverBean.setIsLast(itemSectionListBean.getIsLast());
                                        menuserverBean.setName(itemSectionListBean.getName());
                                        menuserverBean.setSpecialType(itemSectionListBean.getSpecialType());
                                        menuserverBean.setTempletType(itemSectionListBean.getTempletType());
                                        myShowItemListBean.setMenuserver(menuserverBean);
                                        homeModuleFourList.add(sort,myShowItemListBean);
                                        idList.add(myShowItemListBean.getMenuId());
                                        dragGridViewAdapter.notifyDataSetChanged();
                                        editFunctionAdapter.notifyDataSetChanged();
                                    }else {
                                        myToast.showShort("最多只能添加四个功能");
                                    }
                                }
                            });
                        }else {
                        }
                    }
                }
            }else  if(flag.equals(SAVEMYSECTIONFLAG))
            {
                RespondObj respObj = JsonTools.jsonStringToObject(jsonString, RespondObj.class);
                String code = respObj.getCode();
                Object object = respObj.getObj();
                if (code.equals("200") && respObj.getMsg().equals("ok")) {
                    // 保存到本地数据库
/*                    IndexNameSort indexNameSort=indexNameSortDaoUtils.queryMeiziById(Constant.daoServer);
                    if(indexNameSort!=null)
                    {
                        indexNameSortDaoUtils.updateIndexNameSort(new IndexNameSort(Constant.daoServer,userid,JsonTools.objectToJsonString(homeModuleFourList)));
                    }else {
                        indexNameSortDaoUtils.insertIndexNameSort(new IndexNameSort(Constant.daoServer,userid,JsonTools.objectToJsonString(homeModuleFourList)));
                    }*/
                    setResult(100);
                    finish();
                }
            }

        } catch (Exception e) {

        }
    }
}
